<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Caculate the signature
 *
 * @param	array	parameter array
 * @return	string	signature
 */
if ( ! function_exists('cal_sig'))
{
	function cal_sig($array)
	{
		if(empty($array))
		{
			return '';
		}
		
		$result = '';
		foreach ($array as $item)
		{
			$result = $result.'{'.$item.'}';
		}
		
		$result = md5($result, false);
		return $result;
	}
}

/**
 * Compare the device information to identify whether login on another device
 *
 * @param	integer	os. 1: android; 2: ios; 3: windows phone
 * @param	array	device1 info array
 * @param	array	device2 info array
 * @return	boolean compare result. true: same; false: different.
 */
if ( ! function_exists('check_device_info'))
{
	function check_device_info($os, $array1, $array2)
	{
		if(empty($array1) || empty($array2))
			return false;
		
		if($array1['os'] != $array2['os'])
			return false;

		if($os == 1) {
			if(empty($array1['imei']) || empty($array1['mac']) || empty($array1['aid']) ||
					empty($array2['imei']) || empty($array2['mac']) || empty($array2['android_id']))
				return false;
			
			if(strcmp($array1['imei'], $array2['imei']) != 0 ||
					strcmp($array1['mac'], $array2['mac']) != 0 ||
					strcmp($array1['aid'], $array2['android_id']) != 0)
				return false;
		} elseif($os == 2) {
			if(empty($array1['uuid']) || empty($array1['openudid']) || empty($array1['adid']) || empty($array1['idfv']) ||
					empty($array2['uuid']) || empty($array2['openudid']) || empty($array2['adid']) || empty($array2['idfv']))
				return false;
				
			if(strcmp($array1['uuid'], $array2['uuid']) != 0 ||
					strcmp($array1['openudid'], $array2['openudid']) != 0 ||
					strcmp($array1['adid'], $array2['adid']) != 0 ||
					strcmp($array1['idfv'], $array2['idfv']) != 0)
				return false;
		} elseif($os == 3) {
			if(empty($array1['did']) || empty($array2['deviceuniqueid']))
				return false;
				
			if(strcmp($array1['did'], $array2['deviceuniqueid']) != 0)
				return false;
		} else {
			return false;
		}
		
		return true;
	}
}